package com.logic.landseaserver.models.payment.impl;

import java.text.DecimalFormat;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.security.SecurityUtils;
import com.logic.landseaserver.common.LandeaConstants.QueryUesdCodeType;
import com.logic.landseaserver.common.enums.DepositEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.PaymentResultCode;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.domain.Deposit;
import com.logic.landseaserver.domain.Project;
import com.logic.landseaserver.models.authcode.service.AuthCodeService;
import com.logic.landseaserver.models.independent.service.BillIndependentService;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.BillTypeCode;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.service.AbstractPaymentResponseHandler;
import com.logic.landseaserver.models.prepayment.bean.AotuPayBill;
import com.logic.landseaserver.persistence.read.DepositReadMapper;
import com.logic.landseaserver.persistence.read.ProjectReadMapper;
import com.logic.landseaserver.persistence.write.DepositWriteMapper;
import com.logic.landseaserver.service.IDepositService;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：RealBillResponseHandler</br>
 * 类描述：预定账单结果通知处理</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年8月01日 下午4:33:21</br>
 * 
 * @version 1.0
 *
 */
@Service(BillTypeCode.RESERVE + PaymentConstant.PAYMENT_RESPONSE_SUFFIX)
public class ReserveResponseHandler extends AbstractPaymentResponseHandler
{
    @Autowired
    private DepositReadMapper depositReadMapper;
    
    @Autowired
    private DepositWriteMapper depositWriteMapper;
    
    @Autowired
    private ProjectReadMapper projectReadMapper;
    
    @Autowired
    private IDepositService iDepositServiceImpl;
    
    @Autowired
    private BillIndependentService billIndependentService;
    
    @Autowired
    private AuthCodeService authCodeService;
    
    @Override
    protected void doLocalBusiness(BillPayment payment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        String logInfo = getLog(payment, payment.getId());
        
        try
        {
            // 此处预定信息必须存在
            Deposit deposit = depositReadMapper.selectByPrimaryKey(payment.getBillId());
            if (null == deposit)
            {
                // 账单不存在
                LOGGER.error(logInfo + " deposit does not exists in ls_deposit table,depositId=" + payment.getBillId());
                throw LandseaException.createException(PaymentResultCode.E00150002);
            }       
            
            // 生成预定编号
            Project project = projectReadMapper.selectByPrimaryKey(deposit.getPrjId());
            
            Integer maxContractCode = depositReadMapper.getMaxContractCode();
            String depositSn = project.getPrjCode() + new DecimalFormat("000000").format(maxContractCode);
            
            // 更新，只更新必要的字段
            Deposit depositUpdate = new Deposit();
            depositUpdate.setId(deposit.getId());
            depositUpdate.setDepositSn(depositSn);
            depositUpdate.setPayDate(payment.getTime());
            // 更新成未使用
            depositUpdate.setStatus(DepositEnum._DS2.getCode());
            depositUpdate.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
            // 支付方式
            depositUpdate.setPayMethod(payment.getPayType());
            // 交易流水
            depositUpdate.setPaySn(payment.getTransactionNo());
            
            depositWriteMapper.updateByPrimaryKeySelective(depositUpdate);
            
            // 同步更新查询出的对象，为后续PDF使用
            deposit.setDepositSn(depositSn);
            deposit.setPayDate(payment.getTime());
            deposit.setStatus(DepositEnum._DS2.getCode());
            
            //银石里面处理预定的往balancedSheet修改状态
            billIndependentService.depUpdateBalanceSheet(deposit.getId(),depositSn, payment.getTime());
            
            // 更新授权码为已使用状态
            authCodeService.updateCouponStatusAfterPay(QueryUesdCodeType.DEPOSIT, deposit.getId());
            
            // 修改PDF
            iDepositServiceImpl.renamePDF(deposit);
            
            LOGGER.info(logInfo + "update deposit or rename PDF finished.");
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOGGER.error(logInfo + "update deposit or rename PDF occur a error.", e);
            throw LandseaException.createException(PaymentResultCode.E00150030);
        }
    }
    
    @Override
    protected String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }

    /**    
     * [描述]：</br>
     *
     * @param aotuPayBill
     * @param billPayment
     * @throws LandseaException
     *
    */
    @Override
    public void aotoPayDoLocalBusiness(AotuPayBill aotuPayBill, BillPayment billPayment)
        throws LandseaException
    {
        
    }
}
